package cn.ilikes.tools.hbase.jpa.indexs.lucenes;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;
import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.clients.nodes.Node;
import cn.ilikes.tools.hbase.jpa.indexs.Indexer;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.untils.PropertyAccessorHelper;

public class LuceneEntityClassToDocument {

	private static Logger logger = Loggerfactory.instance(LuceneEntityClassToDocument.class);

	/**
	 * 
	 * @param document
	 * @param metadata
	 * @param entity
	 * @param parentId
	 * @param clazz
	 * @return
	 */

	public Document addEntityClassToDocument(Document document, Node node) throws IllegalArgumentException {

		try {
			Object entity = Node.getEntity(node);
			IEntityMetaData metadata = Node.getEntityMetaData(node);
			Object id = PropertyAccessorHelper.getId(entity, metadata);
			Field luceneField = new Field(Indexer.ENTITY_ID_FIELD, id.toString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add  EntityClass  To  Document  lucene field : " + Indexer.ENTITY_ID_FIELD + " value : " + id.toString());
			document.add(luceneField);

			String kunderaId = LuceneQueryUtils.getJPAHBaseKeyofId(metadata, id);
			luceneField = new Field(Indexer.KUNDERA_ID_FIELD, kunderaId, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + Indexer.KUNDERA_ID_FIELD + " value : " + kunderaId);
			document.add(luceneField);

			String entityClazz = metadata.getEntityClazz().getCanonicalName();
			luceneField = new Field(Indexer.ENTITY_CLASS_FIELD, entityClazz, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + Indexer.ENTITY_CLASS_FIELD + " value : " + entityClazz);
			document.add(luceneField);

			String time = Long.toString(System.currentTimeMillis());
			luceneField = new Field(Indexer.KUNDERA_INDEX_TIMESAMP, time, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			document.add(luceneField);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + Indexer.KUNDERA_INDEX_TIMESAMP + " value : " + time);

			String indexName = metadata.getEntityIndex().getIndexName();
			luceneField = new Field(Indexer.ENTITY_INDEXNAME_FIELD, indexName, Field.Store.NO, Field.Index.NOT_ANALYZED);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + Indexer.ENTITY_INDEXNAME_FIELD + " value : " + indexName);
			document.add(luceneField);

			return document;
		} catch (Exception e) {
			Loggerfactory.error(logger, e);
			throw new IllegalArgumentException("add entity class   to   document  error : " + e.getMessage(), e);
		}
	}

}